package com.xms.security.service.principal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.config.FinalValueDefaults;
import com.xms.security.core.RoleMemberShipType;
import com.xms.security.entity.RoleMemberShip;
import com.xms.security.service.role.IRoleMemberShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author migo
 * @since 2021-08-29
 */
@Service
public class SystemUserRolesService implements ISystemUserRolesService {
    private IRoleMemberShipService _roleMemberShipService;
    @Autowired
    public SystemUserRolesService(IRoleMemberShipService roleMemberShipService){
        _roleMemberShipService = roleMemberShipService;
    }

    @Override
    public List<RoleMemberShip> getByUserId(UUID systemUserId) {
        LambdaQueryWrapper<RoleMemberShip> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMemberShip::getMemberid, systemUserId)
        .eq(RoleMemberShip::getTypecode, RoleMemberShipType.USER.value());
        return _roleMemberShipService.list(queryWrapper);
    }

    @Override
    public boolean isAdministrator(UUID systemUserId) {
        if(systemUserId.equals(FinalValueDefaults.ADMIN_USERID))
            return true;
        LambdaQueryWrapper<RoleMemberShip> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMemberShip::getMemberid, systemUserId)
                .eq(RoleMemberShip::getTypecode, RoleMemberShipType.USER.value())
                .eq(RoleMemberShip::getRoleid, FinalValueDefaults.ADMIN_ROLEID);
        return _roleMemberShipService.count(queryWrapper) > 0;
    }
}
